<?php
/**
*作用:
*说明:
*版权:
*作者:Red	QQ:316765128
*时间:2011/7/28
**/
include_once './include/init.inc.php';
include_once './include/sphinxapi.class.php';


$re = shpinxIn('上海');
print_r($re);
echo "<br>";

$re = aroundSearch('39.979128','116.313681',100);
print_r($re);

function shpinxIn($q,$index='shop',$limit=10,$match=SPH_MATCH_ALL,$sort=SPH_SORT_RELEVANCE,$rank=SPH_RANK_PROXIMITY_BM25){
	$cl = new SphinxClient();
	$cl->SetServer(get_client(),3312);
	$cl->SetMatchMode($match);		//设定搜索模式(默认=>完整匹配)
	$cl->SetSortMode($sort);		//设置匹配项的排序模式(默认=>按相关度降序排列)
	$cl->SetRankingMode($rank);		//同时使用词组评分和BM25评分
	$cl->SetArrayResult(true);		//PHP专有，以数组方式显示出来
	$cl->SetLimits(0,(int)$limit);
	//$cl->SetFilter($q,10);
	$res = $cl->Query($q,$index);
	echo $cl->GetLastError();		//输出错误信息
	echo $cl->GetLastWarning();		//输出警告信息
	$ids = '0';
	if($limit == '1'){
		if(!empty($res['matches'])){
			$ids = $res['matches']['0']['id'];
		}
	}else{
		if(!empty($res['matches'])){
			foreach($res['matches'] as $key=>$val){
				$ids .= ','.$val['id'];
			}
		}
	}
	$totle = empty($res['total_found'])?'0':$res['total_found'];
	Return array('ids'=>$ids,'totle'=>$totle);
}
function aroundSearch($x=null,$y=null,$k=3,$limit=20,$s=1,$q=''){
	$cl = new SphinxClient();
	$cl->SetServer(get_client(),3312);
	$cl->SetMatchMode(SPH_MATCH_ALL);		//设定搜索模式(默认=>完整匹配)
	$cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);	//同时使用词组评分和BM25评分
	$cl->SetArrayResult(true);
	$cl->SetLimits(0,(int)$limit);
	$cl->SetGeoAnchor("latitude","longitude",(float) deg2rad($x),(float) deg2rad($y));
	$cl->SetFilterFloatRange('@geodist', (float)0, (float)1000*$k);	//设置范围
	$cl->SetSortMode(SPH_SORT_EXTENDED,'@geodist ASC,@id DESC');	//按照距离由近及远
	$res = $cl->Query($q,'shop');
	$ids = '0';
	if(!empty($res['matches'])){
		$i=0;
		foreach($res['matches'] as $key=>$val){
			empty($i) ? $ids .= $val['id'] : $ids .= ','.$val['id'];
			++$i;
		}
	}
	$totle = empty($res['total_found'])?'0':$res['total_found'];
	Return array('ids'=>$ids,'totle'=>$totle);
}
function get_client(){
	$ip = '118.192.24.158';
	Return $ip;
}
//echo date("Y-m-d H:i:s",'1325088000');
exit();

////////////////////////////////////////////////////////////////////////////////////// sphinx test
/////////////////////////////////////////////
if(!empty($pet_id)){		//修改宠物资料
	
}
$sid2 = '';
$sort = array('64'=>'大型犬','65'=>'中型犬','66'=>'小型犬','3'=>'猫咪','44'=>'水族');
$sz_arr = array(115,116,117,118,119,120,121,122,123,124,125,126);
if(empty($goods_id)){
	$goods_id = '';
	$list_goodsname = '"==请选择分类==":{"key":" ","defaultvalue" : "","values":{"==品种==":"==品种=="}}';
}else{	//获取分类
	is_numeric($goods_id)?'':$goods_id='';
	$sql = "select `goods_name`,`cat_id` from `info_goods` where `goods_id`='$goods_id'";
	$res = $db->fetch_one($sql);
	if(!empty($res)){
		$sid2 = $goods_id;			//品种ID
		in_array($res['cat_id'],$sz_arr)?$sort_id='44':$sort_id=$res['cat_id'];	//获取分类ID
		$list_goodsname = '"'.$sort[$sort_id].'":{"key":"'.$sort[$sort_id].'","defaultvalue" : ""}';
	}else{
		$list_goodsname = '"==请选择分类==":{"key":" ","defaultvalue" : "","values":{"==品种==":"==品种=="}}';
	}
}

foreach($sort as $key=>$val){
	$sql = "select `goods_name`,`goods_id` from `info_goods` WHERE ";
	if($key=='44'){
		$sql .= "cat_id in (115,116,117,118,119,120,121,122,123,124,125,126)";
	}else{
		$sql .= 'cat_id='.$key;
	}
	$res = $db->fetch_all($sql);
	$list_goodsname .= ',';
	$list_goodsname .= '"'.$val.'":{';
	$list_goodsname .= '"key":"'.$val.'",';
	$list_goodsname .= '"defaultvalue" : "'.$sid2.'",';
	$list_goodsname .= '"values":{';
	$li = '';
	foreach($res as $key2=>$val2){
		$li .= ',"'.$val2['goods_name'].'":"'.$val2['goods_id'].'"';
	}
	if(!empty($li)){
		$li = substr($li,1);
	}
	$list_goodsname .= $li.'}}';
}
$smarty->assign('list_goodsname',$list_goodsname);
$smarty->assign('goods_id',$goods_id);
$smarty->display('red.dwt');
//////////////////////////////////////////////////////////////////////////////////////////////////////

exit();
//最后一次登陆时间
$db -> query("update `info_link_user`  set `lastlogin` = `dateline`");
exit();

//数据库变更
$db -> query("update info_user_pet as p set goods_name = ( select goods_name from info_goods as g where g.goods_id = p.goods_id)");
exit();
//运行转换函数
/*
insert into `info_goods_qlike`(`goods_id`,`user_id`) select a.`goods_id`,u.`id` as user_id from `info_goods_like` as a left join `info_link_user` as u on a.`app_openid` = u.`app_openid` where a.`app_openid`<>'' and u.id<>''

select count(*) from `info_goods_qlike`
*/
// `info_goods_like` 取消了 app_openid

exit();

$a = $db -> fetch_all("select p.`id`,p.`app_openid`,u.`id` as user_id from `info_user_pet` as p left join `info_link_user` as u on p.`app_openid`=u.`app_openid` where u.`id`<>''");

foreach($a as $key=>$val){
	$u = $db -> query("update info_user_pet set `user_id` = ".$val['user_id']." where `id`='".$val['id']."'");
}
exit();

$res = $db -> fetch_all("select goods_id,goods_name from info_goods");
foreach($res as $key=>$val){
	echo "insert into info_goods(id,goods_name) values('".$val['goods_id']."','".$val['goods_name']."');";
}
exit();